package com.njy.aop.test;

import java.util.concurrent.*;

public class FutureTest1 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        FutureTask<String> f1 = new FutureTask<>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                System.out.println("t1洗水壶");
                TimeUnit.SECONDS.sleep(1);
                System.out.println("t1烧开水");
                TimeUnit.SECONDS.sleep(8);
                return "t1开水备好";
            }
        });
        FutureTask f2 = new FutureTask<String>(new task2());
        executorService.submit(f1);
        executorService.submit(f2);
        System.out.println(f1.get()+f2.get());
        System.out.println("开始泡茶");
       // executorService.shutdown();
    }

    private static class task2 implements Callable<String> {
        @Override
        public String call() throws Exception {

            System.out.println("t2洗茶壶");
            TimeUnit.SECONDS.sleep(1);
            System.out.println("t2洗茶杯");
            TimeUnit.SECONDS.sleep(1);
            System.out.println("t2拿茶叶");
            TimeUnit.SECONDS.sleep(2);
            return "t2拿到绿茶";
        }
    }
}
